<template>
	<view class="myExchangePage">
		<view class="tabContent">
			<view @click="changeTab(item.id)" v-for="(item,index) in tabs" :key="index" class="tabItem"
				:class="activeTab===item.id?'isActive':''">{{item.name}}</view>
		</view>
		<view class="newsDetial" v-if="isLoad">
			<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" v-if="!list.length" text="没有兑换记录">
			</u-empty>
			<view v-for="(item,index) in list" :key="index" class="vList" v-if="activeTab==1 || activeTab==4">
				<view class="vImage">
					<u--image :src="baseUrl+'/'+item.images" shape="square" mode="aspectFill" width="280rpx"
						height="192rpx" radius="12rpx">
					</u--image>
				</view>
				<view class="vInfo">
					<view class="title">{{item.title}}</view>
					<view class="integral">兑换：{{item.integral}}积分</view>
					<view class="vBtn" @click="doChange(item)">发送邮箱</view>
				</view>
			</view>
			<view class="documentList" v-for="(item,index) in list" :key="index" v-if="activeTab=='2'">
				<view class="status" v-if="item.isExchange">
					<u--image src="/static/vOnTrial/1.png" shape="square" mode="aspectFill" width="82rpx"
						height="82rpx">
					</u--image>
				</view>
				<view class="image">
					<u--image :src="getImgUrl(item.uploadUrl)" shape="square" mode="aspectFill" width="116rpx"
						height="116rpx">
					</u--image>
				</view>
				<view class="documentInfo">
					<view class="title">{{item.curriculumName}}</view>
					<view class="info">
						<view class="author">{{item.curriculumAuthor}}</view>
						<view class="createtime">{{item.createTime}}</view>
					</view>
					<view class="cost">兑换：{{item.integral||0}}积分</view>
				</view>
				<view class="checkDocBtn" @click="openFile(item.id, item.uploadUrl)">在线预览</view>
				<view class="documentBtn" @click.stop="doChange(item)">发送邮箱</view>
			</view>
			<view class="documentList" v-for="(item,index) in list" :key="index" v-if="activeTab=='3'">
				<view class="documentInfo">
					<view class="title">{{item.courseName}}</view>
					<view class="info">
						<view class="createtime">{{item.createTime}}</view>
					</view>
					<view class="playbackVolume">播放：{{item.playbackVolume||0}}</view>
					<view class="cost costPos">{{item.vpoints}}积分</view>
				</view>
				<view class="documentBtn" @click="doVideo(item)">播放</view>
			</view>
			<view v-for="(item,index) in list" :key="index" class="vList" v-if="activeTab==5">
				<view class="vImage">
					<u--image :src="item.picUrl" shape="square" mode="aspectFill" width="280rpx"
						height="192rpx" radius="12rpx">
					</u--image>
				</view>
				<view class="vInfo">
					<view class="title">{{item.name}}</view>
					<view class="integral">兑换：{{item.actualPrice}}积分</view>
					<view class="vBtn" @click="doOrder(item.orderId, item.name, item.picUrl)">订单详情</view>
				</view>
			</view>
			<u-loadmore v-if="list.length" :status="isFinish?'nomore':'loading'" loading-text="正在加载"
				loadmore-text="加载更多" nomore-text="没有更多了" color="#d0d0d0" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				api: this.$api.baseUrl,
				baseUrl: this.$api.baseUrl,
				list: [],
				pageNo: 1,
				pageSize: 10,
				total: 0,
				userId: "",
				isFinish: false,
				isLoading: false,
				isLoad: false,
				activeTab: "5",
				tabs: [{
					name: "V商城",
					id: "5"
				},{
					name: "V+安装包",
					id: "1"
				}, {
					name: "V+月度授权",
					id: "4"
				}, {
					name: "文档资料",
					id: "2"
				}, {
					name: "视频中心",
					id: "3"
				}],
				myEmail: ""
			}
		},
		computed: {
			getReleaseTime(date) {
				return function(date) {
					if (date) {
						if (typeof date === 'number') {
							return new Date(date).format("YYYY-MM-dd hh:mm")
						} else {
							return new Date(date.replace(/\-/g, "/")).format("YYYY-MM-dd hh:mm")
						}
					} else {
						return ""
					}
				}
			},
		},
		onReachBottom() {
			if (!this.isLoading && !this.isFinish) {
				this.pageNo++;
				this.initData();
			}
		},
		onPullDownRefresh() {
			this.doRefresh();
		},

		onLoad() {
			uni.getStorage({
				key: "userInfo",
				success: (userInfo) => {
					this.userId = userInfo.data.id;
					this.myEmail = userInfo.data.educationMailbox;
					console.log(this.myEmail);
					this.initData()
				}
			})
		},
		onShow() {
			this.refreshUser()
		},
		methods: {
			doRefresh() {
				this.isFinish = false;
				this.isLoad = false;
				if (!this.isLoading) {
					this.pageNo = 1;
					this.list = [];
					this.initData(true);
				}
			},
			refreshUser() {
				if (this.isLoad) {
					this.$request.get(this.$api.getUserById, {
						id: this.userId
					}).then(result => {
						uni.hideLoading()
						uni.setStorage({
							key: "userInfo",
							data: result.result
						})
						uni.hideLoading()
						this.userScore = result.result.integral;
						this.myEmail = result.result.educationMailbox;
					})
				}
			},
			doChange(item) {
				if (this.myEmail) {
					if (this.activeTab == 1) {
						this.$request.get(this.$api.sendEmail, {
							email: this.myEmail,
							title: item.title,
							content: "V+软件下载链接：" + item.link
						}).then(res => {
							uni.showToast({
								icon: "success",
								title: "发送成功"
							})
							uni.hideLoading()
						})
					}
					if (this.activeTab == 4) {
						let fileArr = item.fileList.split("/")
						this.$request.get(this.$api.sendEmailFile, {
							email: this.myEmail,
							title: item.title,
							filename: fileArr[fileArr.length - 1],
							filepath: this.$appFunc.getFileUrl(item.fileList),
							content: "点击附件下载文件"
						}).then(res => {
							uni.showToast({
								icon: "success",
								title: "发送成功"
							})
							uni.hideLoading()
						})
					}
					if (this.activeTab == 2) {
						this.$request.get(this.$api.sendEmailFile, {
							email: this.myEmail,
							title: item.curriculumName,
							content: "点击附件下载文件",
							filepath: this.$appFunc.getFileUrl(item.uploadUrl),
							filename: item.uploadUrl
						}).then(res => {
							uni.showToast({
								icon: "success",
								title: "发送成功"
							})
							uni.hideLoading()
						})
					}
					if (this.activeTab == 3) {
						this.$request.get(this.$api.sendEmail, {
							email: this.myEmail,
							title: item.curriculumName,
							// content: "视频下载地址：" + this.baseUrl + "/" + item.uploadUrl
							content: "视频下载地址：" + this.$appFunc.getFileUrl(item.uploadUrl)
						}).then(res => {
							uni.showToast({
								icon: "success",
								title: "发送成功"
							})
							uni.hideLoading()
						})
					}
				} else {
					uni.showModal({
						title: '温馨提示',
						content: '您还没有绑定邮箱，请去个人中心绑定邮箱账号',
						confirmText: "前去绑定",
						cancelText: "放弃领取",
						success: (res) => {
							if (res.confirm) {
								uni.navigateTo({
									url: "/pages/myInfo/myInfo"
								})
							} else if (res.cancel) {
								uni.showToast({
									icon: "none",
									title: "已取消发送"
								})
							}

						}
					})
				}
			},
			doOrder(id, name, picUrl) {
				uni.navigateTo({
					url: '/pages/orderDetail/orderDetail?id='+id+'&from=list'
				})
			},
			getImgUrl(item) {
				let arr = item.split(".");
				let type = arr[arr.length - 1];
				let imageUrl = "";
				switch (type) {
					case "zip":
						imageUrl = '/static/growup/2.png'
						break;
					case "doc":
						imageUrl = '/static/growup/3.png'
						break;
					case "docx":
						imageUrl = '/static/growup/3.png'
						break;
					case "pdf":
						imageUrl = '/static/growup/4.png'
						break;
					case "xls":
						imageUrl = '/static/growup/5.png'
						break;
					case "xlsx":
						imageUrl = '/static/growup/5.png'
						break;
					case "ppt":
						imageUrl = '/static/growup/6.png'
						break;
					default:
						imageUrl = '/static/growup/8.png'
						break;
				}
				return imageUrl
			},
			changeTab(id) {
				this.activeTab = id;
				this.doRefresh();
			},
			initData(isRefresh) {
				this.isLoading = true;
				Promise.all([
					this.$request.get(this.$api.myExchange, {
						userId: this.userId,
						exchangeType: this.activeTab,
						// pageNo: this.pageNo,
						// pageSize: this.pageSize
					}),
				]).then(res => {
					if (isRefresh) {
						uni.stopPullDownRefresh()
					}
					this.isLoading = false;
					this.list = this.list.concat(res[0].result.records)
					this.total = res[0].result.total;
					if (this.pageNo * this.pageSize < this.total) {
						this.isFinish = false
					} else {
						this.isFinish = true
					}
					this.isLoad = true;
					uni.hideLoading()
				})
			},
			doVideo(item) {
				uni.navigateTo({
					url: '/pages/studyDetail/studyDetail?id=' + item.curriculumId
				})
			},
			goDetail(item) {
				if (this.activeTab === "1") {
					uni.navigateTo({
						url: "/pages/solutionDetial/solutionDetial?id=" + item.otherId,
					})
				} else if (this.activeTab === "2") {
					uni.navigateTo({
						url: "/pages/newsDetial/newsDetial?id=" + item.otherId,
					})
				} else if (this.activeTab === "3") {
					uni.navigateTo({
						url: "/pages/postDetail/postDetail?id=" + item.otherId,
					})
				} else if (this.activeTab === "4") {

				}

			},
			openFile(id, url) {
				uni.showLoading({
					title: '文件加载中',
					mask: true
				})
				this.$appFunc.openFile(id, url).then(res=> {
					uni.hideLoading()
				}).catch((e) => {
					uni.hideLoading()
					this.$appFunc.msg('打开失败')
				})
			}
		}
	}
</script>

<style lang="scss">
	.myExchangePage {
		padding: 0 40rpx 40rpx 40rpx;
		.tabContent {
			display: flex;
			flex-wrap: nowrap;
			justify-content: flex-start;
			width: calc(100vw - 80rpx);
			overflow: auto;
			padding: 20rpx 0;

			.tabItem {
				background: #F4F4F4;
				border-radius: 8rpx;
				color: #333333FF;
				font-size: 26rpx;
				height: 52rpx;
				padding: 0 40rpx;
				line-height: 52rpx;
				margin-right: 20rpx;
				flex-shrink: 0;
			}

			.isActive {
				color: #004DA1FF;
				background-color: #E5EDFFFF;
			}
		}

		.solutionList {
			display: flex;
			flex-direction: column;
			padding: 30rpx 20rpx 24rpx;
			background: #FFFFFF;
			box-shadow: inset 0px 0px 0px 1rpx rgba(230, 230, 230, .5);
			border-radius: 10px 10px 10px 10px;
			margin-bottom: 20rpx;

			.itemTop {
				display: flex;
				flex-wrap: nowrap;
				justify-content: flex-start;
				padding-bottom: 20rpx;

				.topLeft {
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					width: 100%;

					.title {
						font-family: PingFang SC-Bold, PingFang SC;
						font-weight: bold;
						color: #333333;
						font-size: 28rpx;
						letter-spacing: 1rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 3;
						-webkit-box-orient: vertical;
						padding-right: 20rpx;
					}

					.talk {
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #999999;
						font-size: 26rpx;
					}
				}

				.topRight {
					flex-shrink: 0;
					padding-right: 20rpx;
				}
			}

			.itemBottom {
				display: flex;
				flex-wrap: nowrap;
				justify-content: space-between;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
				font-size: 26rpx;

				.operate {
					display: flex;
					flex-wrap: nowrap;
					justify-content: flex-start;
					width: 100%;

					.item {
						display: flex;
						flex-wrap: nowrap;
						align-items: center;
						padding-right: 40rpx;

						.count {
							padding-left: 10rpx;
						}
					}
				}
			}
		}

		.documentList {
			background: #FFFFFF;
			box-shadow: inset 0px 0px 0px 1rpx rgba(230, 230, 230, .5);
			border-radius: 20rpx;
			margin-bottom: 20rpx;
			display: flex;
			flex-wrap: nowrap;
			justify-content: flex-start;
			align-items: center;
			padding: 30rpx 26rpx;
			position: relative;

			.status {
				position: absolute;
				top: 0;
				left: 0;
			}

			.image {
				margin-right: 26rpx;
			}

			.documentInfo {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				height: 126rpx;

				.title {
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: bold;
					color: #333333;
					font-size: 26rpx;
				}

				.info {
					font-size: 26rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #666666;
					display: flex;
					flex-wrap: nowrap;

					.author {
						position: relative;
						padding-right: 8rpx;

						&:after {
							content: "";
							display: inline-block;
							height: 22rpx;
							width: 2rpx;
							background-color: #BDBDBDFF;
							position: absolute;
							right: 0;
							top: calc(50% - 11rpx);
						}
					}

					.createtime {
						font-size: 26rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #666666;
					}
				}

				.cost {
					font-size: 26rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #EC6C2F;
				}

				.playbackVolume {
					font-size: 24rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #999999;
				}

				.costPos {
					position: absolute;
					right: 26rpx;
				}
			}

			.documentBtn {
				position: absolute;
				right: 26rpx;
				bottom: 30rpx;
				background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
				border-radius: 34rpx;
				height: 50rpx;
				width: 136rpx;
				line-height: 50rpx;
				font-size: 24rpx;
				color: #fff;
				text-align: center;
			}
			.checkDocBtn{
				position: absolute;
				right: 174rpx;
				bottom: 30rpx;
				background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
				border-radius: 34rpx;
				height: 50rpx;
				width: 136rpx;
				line-height: 50rpx;
				font-size: 24rpx;
				color: #fff;
				text-align: center;
			}
		}

		.vList {
			display: flex;
			flex-wrap: nowrap;
			margin-bottom: 20rpx;
			padding: 32rpx 20rpx;
			background-color: #fff;
			box-shadow: inset 0px 0px 0px 1rpx rgba(230, 230, 230, 0.5);
			border-radius: 20rpx;
			position: relative;

			.tag {
				position: absolute;
				left: 0;
				top: 0;
				z-index: 1;
			}

			.vImage {
				padding-right: 20rpx;
			}

			.vInfo {
				display: flex;
				justify-content: space-between;
				flex-direction: column;

				.title {
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: bold;
					color: #333333;
					font-size: 28rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 3;
					-webkit-box-orient: vertical;
				}

				.integral {
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #EC6C2F;
					font-size: 26rpx;
				}

				.vBtn {
					background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
					width: 136rpx;
					height: 50rpx;
					border-radius: 34rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
					font-size: 24rpx;
					line-height: 50rpx;
					text-align: center;
				}
			}
		}
	}
</style>
